<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="myForm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="myToolbarElem">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"> 添加用户</button>
            </div>
        </script>

        <table class="layui-hide" id="myTableElem" lay-filter="myTableFilter"></table>

        <script type="text/html" id="myTableBarElem">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="reset">密码重置</a>
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="google">设置验证码</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="clear">删除</a>
        </script>
        <div class="code" id="code">
            <label></label>
        </div>

        <div class="barcode center" style="display: none;">
            <img id="barcode">
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../lib/qrcode/qrcode.js" charset="utf-8"></script>
<script src="../../js/layutil.js" charset="utf-8"></script>
<script src="../../js/col-utils.js" charset="utf-8"></script>
<script src="../../lib/qrcode/JsBarcode.all.min.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.$,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;
        var tplRoleId = function (d) {
            if (d.roleId == '1') {
                return "<span style='color: green'>普通用户</span>";
            } else if (d.roleId == '2') {
                return "<span style='color: blue'>管理员</span>";
            } else {
                return "<span style='color: red'>角色未配置</span>";
            }
        };
        layutil.table('/myerp/user/list', colsutil.user_cols(tplRoleId));

        /**
         * toolbar监听事件
         */
        table.on('tool(myTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'reset') {  // 监听添加操作
                var str = "确定重置密码？";
                layer.confirm(str, {btn: ['确定', '取消'], title: "提示"}, function (index) {
                    layutil.postForm("/myerp/user/reset_password",{id : data['id']});
                })
            } else if (obj.event === 'google') {
                var str = "确定设置谷歌验证码？";
                layutil.confirm(str, function () {
                    layutil.postForm("/myerp/user/auth_code",{id : data['id']}, function (data) {
                        console.log(data);
                        var map = data.data;
                        var url_ = map.url;
                        var key_ = map.key;
                        layer.open({
                            title: false,
                            type: 1,
                            shade: 0.3,
                            closeBtn: 0,
                            shadeClose: true,
                            area: ['158px','178px'],
                            content: $(".code"),
                            offset: ['180px','500px'],
                            success: function (layero, index) {
                                qrcode(url_);
                                $(".code label").html(key_);
                            }
                        });
                    });
                });

            } else if (obj.event === 'clear') {
                var str = "确定删除用户？";
                layutil.confirm(str, function () {
                    layutil.postForm("/myerp/user/delete",{id : data['id']},function () {
                        table.reload('myTableElem');
                    });
                });
            }
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(myTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/sys_user/user-add.html',
                    end: function () {//无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
                        table.reload('myTableElem');
                    }
                });
            }
        });

        var qrcode_obj = null;
        var qrcode = function (url) {
            if(qrcode_obj != null) {
                qrcode_obj.clear();
                qrcode_obj.makeCode(url);
            } else {
                qrcode_obj = new QRCode('code', {
                    text: url,
                    width: 135,
                    height: 135,
                    colorDark : '#000000',
                    colorLight : '#ffffff',
                    correctLevel : QRCode.CorrectLevel.H
                });
            }
            $("#code").attr("title", "底部是谷歌验证码");
            $("#code").show();
        };
        //https://github.com/lindell/JsBarcode
        var barcode = function(data) {
            JsBarcode('#barcode', "1234",{
                format: "pharmacode", // 选择要使用的条形码类型
                lineColor: "#0aa",
                width: 4,
                height: 40,
                displayValue: false // 是否在条形码下方显示文字 如果压缩图片数字会变形，可设置false，用css样式调整数字的位置
            });
            $("#code").show();
        }

    });
</script>

</body>
</html>